package com.sharefang.aproject.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sharefang.aproject.BaseApplication;
import com.sharefang.aproject.R;
import com.sharefang.aproject.activity.MainActivity;
import com.sharefang.aproject.adapter.FragmentAdapter;
import com.sharefang.aproject.bean.UserBean;
import com.sharefang.aproject.constant.CacheKey;
import com.sharefang.aproject.constant.UserNppType;
import com.sharefang.aproject.net.NetApi;

/**
 * Created by Administrator on 2016/8/12.
 */

@SuppressLint("ValidFragment")
public class UserFragment extends Fragment implements CacheKey, NetApi {
    private Context context;
    ViewPager viewPager;
    UserBean userBean;
//    UserResultPresenter resultPresenter;

    public ViewPager getViewPager() {
        return viewPager;
    }

    public UserFragment(final Context context) {
        this.context = context;

        this.userBean = BaseApplication.getInstance().getUserBean();//(UserBean) CacheHelper.getAsObject(USER_BEAN);
//        if (userBean == null) {
//            Intent intent = new Intent(context, LoginActivity.class);
//            startActivity(intent);
//            ((Activity) context).finish();
//
//        }

//        if (userBean == null) {
//            resultPresenter = new UserResultPresenter(context, this);
//            resultPresenter.getUserInfo(-1);
//        } else {
        setUserInfo();

//        }

    }

    private void setUserInfo() {
        MainActivity instance = (MainActivity) context;
        instance.setTv_author(userBean.getNickname());
        instance.setTv_description(userBean.getSignature());
        instance.setGuanzhu(userBean.getFollowNum());
        instance.setFuns(userBean.getFollowedNum());
        instance.setHeaderImage(userBean.getHeaderUrl());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_viewpager, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = (ViewPager) view.findViewById(R.id.vp_society);
        setupViewPager(viewPager);

    }


    void setupViewPager(ViewPager viewPager) {
        viewPager.setOffscreenPageLimit(3);
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getChildFragmentManager());
        fragmentAdapter.addFragment(new UserNppFragment(context, UserNppType.UPLOADED), "发布\n" + userBean.getNppNum());
        fragmentAdapter.addFragment(new UserNppFragment(context, UserNppType.ABORT), "未发布\n" + 0);
        fragmentAdapter.addFragment(new UserNppFragment(context, UserNppType.FAVORITE), "收藏\n" + userBean.getFavoriteNum());

        viewPager.setAdapter(fragmentAdapter);
    }

//    @Override
//    public void doUserInfo(UserBean userBean) {
//        CacheHelper.put(USER_BEAN, userBean);
//        this.userBean = userBean;
//        setUserInfo();
//    }
//
//    @Override
//    public void showFailedError(String message) {
//        UiUtil.snake((Activity) context, message);
//    }
}
